#!/bin/sh
#

# redirect errors to a file in user's home directory if we can

errfile="$HOME/.xsession-errors"
if ( umask 077 && cp /dev/null "$errfile" 2> /dev/null )
then
	exec > "$errfile" 2>&1
else

	mktemp=/usr/bin/mktemp
	for errfile in "${TMPDIR-/tmp}/xses-$USER" "/tmp/xses-$USER"
	do
		if ef="$( umask 077 && $mktemp "$errfile.XXXXXX" 2> /dev/null)"
		then
			exec > "$ef" 2>&1
			mv "$ef" "$errfile" 2> /dev/null
			break
		fi
	done
fi

case $# in
1)
	case $1 in
	failsafe)
		exec /usr/bin/xterm -geometry 80x24-0-0
		;;
	esac
esac

# The startup script is not intended to have arguments.

startup=$HOME/.xsession
resources=$HOME/.Xresources
xinitrc=$HOME/.xinitrc

if [ -s "$startup" ]; then
	exec /bin/sh -l "$startup"
else
	if [ -r "$resources" ]; then
		/usr/bin/xrdb -load "$resources"
	fi
	if [ -r "$xinitrc" ]; then
		exec /bin/sh -l "$xinitrc"
	else
		exec /bin/sh -l /etc/X11/xinit/xinitrc
        fi
fi
